/************************************************************
FUNCTION: Checks to see if player and enemy is in line of sight
INPUT:	 Inputs knight and warrior entity
OUTPUT:  Outputs True or False
*************************************************************/
bool checkBounds(Entity knight, Entity warrior)
{
	if(	(abs((knight.getEntityAt() - warrior.getEntityAt())) == 7) ||
	(abs((knight.getEntityAt() - warrior.getEntityAt())) == 6) ||
	(abs((knight.getEntityAt() - warrior.getEntityAt())) == 5) ||
	(abs((knight.getEntityAt() - warrior.getEntityAt())) == 1))
	{
		return true;
	}
	else
	{
		return false;
	}
}

/************************************************************
FUNCTION: Takes which team their on and displays their menu
INPUT:	 Inputs knight and warrior entity, turn
OUTPUT:  choice
*************************************************************/
int getChoice(Entity knight, Entity warrior, int turn)
{
	int choice = 0;

	if(turn == 1)
	{
		cout << "Knight's Turn" << endl;
	}
	else
	{
		cout << "Warrior's Turn" << endl;
	}
	
	cout << "1 - Attack" << endl;
	cout << "2 - Magic" << endl;
	cout << "3 - Move" << endl; //TJD
	cout << "4 - Idle" << endl; //TJD
	cout << "Enter your choice: ";
	cin >> choice;

	return choice;
}


/************************************************************
FUNCTION: Displays and calculates fight mechanics
INPUT:	 Inputs knight and warrior entity
OUTPUT:  VOID
*************************************************************/
void fight(Entity knight, Entity warrior)
{
	int choice = 0;
	srand ((unsigned)time(NULL)); //TJD -- ADDED unsign() to get rid of warning

	while (true) //TJD
	{
		//DISPLAYS MAP
		createMap(knight, warrior);

/*********************************************************************************************
										KNIGHT TURN
**********************************************************************************************/
		if (knight.getHp() > 0)
		{
			
		//DISPLAY MENU
		choice = getChoice(knight,warrior, 1);

		system("CLS");
			
		//ATTACK MELEE
			if (choice == 1)
			{
				//CHECKS IF ENEMY IS IN LOS
				if(checkBounds(knight, warrior) == TRUE)
				{
					knight.attack();
					warrior.defend();

				if (knight.getAttackDamage() > warrior.getArmorLevel())
					warrior.minusHp(knight.getAttackDamage() - warrior.getArmorLevel());
				
					cout << "Warrior Hp: " << warrior.getHp() << endl;
				}
				//ELSE TELLS THEM NO
				else
				{
					cout << "Enemy not in line of sight!" << endl;
				}

			}

		//ATTACK MAGIC
			if (choice == 2)
			{
				//CHECKS IF ENEMY IS IN LOS
				if(checkBounds(knight, warrior) == TRUE)
				{
				knight.magicAttack();
				warrior.resist();

				if (knight.getMagicDamage() > warrior.getResistLevel())
					warrior.minusHp(knight.getMagicDamage() - warrior.getResistLevel());

				cout << "Warrior Hp: " << warrior.getHp() << endl;
				}
				//ELSE TELLS THEM NO
				else
				{
					cout << "Enemy not in line of sight!" << endl;
				}
			}
			
		//MOVE PLAYER
			if (choice == 3)
			{
			createMap(knight, warrior);  //TJD
			int moveTo = knight.getMove();

			// -tjd- Define movement of player

			if (((abs(moveTo - board[knight.getEntityAt()]) % 100) == 0) && ((abs(moveTo - board[knight.getEntityAt()]) / 100) == 1) || //vertical
			((abs(moveTo - board[knight.getEntityAt()]) % 1) == 0) && ((abs(moveTo - board[knight.getEntityAt()]) / 1) == 1) || //horizontal
			((abs(moveTo - board[knight.getEntityAt()]) % 99) == 0) && ((abs(moveTo - board[knight.getEntityAt()]) / 99) == 1) || 
			((abs(moveTo - board[knight.getEntityAt()]) % 101) == 0) && ((abs(moveTo - board[knight.getEntityAt()]) / 101) == 1))
			{
				moveTo = moveTo / 100 * boardSize + moveTo % 100;
				knight.setEntityAt(moveTo);
			}
			else
			cout << "inValid Move " << endl ;
			}
			
		}
		//IDLE
		else
			choice = 4;
		
		system("PAUSE");
		
		//DISPLAYS MAP
		createMap(knight, warrior);


/*********************************************************************************************
										WARRIOR TURN
**********************************************************************************************/

		if (warrior.getHp() > 0)
		{
			choice = getChoice(knight,warrior, 2);
			system("CLS");
			
			//ATTACK MELEE
			if (choice == 1)
			{
				//CHECKS IF ENEMY IS IN LOS
				if(checkBounds(knight, warrior) == TRUE)
				{
					warrior.attack();
					knight.defend();

				if (warrior.getAttackDamage() > knight.getArmorLevel())
					knight.minusHp(warrior.getAttackDamage() - knight.getArmorLevel());
				
				cout << "Knight Hp: " << knight.getHp() << endl;
				}
				//ELSE TELLS THEM NO
				else
				{
					cout << "Enemy not in line of sight!" << endl;
				}
			}
			
			//MAGIC ATTACK
			if (choice == 2)
			{
				//CHECKS IF ENEMY IS IN LOS
				if(checkBounds(knight, warrior) == TRUE)
				{
					warrior.magicAttack();
					knight.resist();

				if (warrior.getMagicDamage() > knight.getResistLevel())
					knight.minusHp(warrior.getMagicDamage() - knight.getResistLevel());

				cout << "Knight Hp: " << knight.getHp() << endl;
				}
				//ELSE TELLS THEM NO
				else
				{
					cout << "Enemy not in line of sight!" << endl;
				}
			}
			cout << endl;
			
			//MOVE PLAYER
			if (choice == 3)
			{
			createMap(knight, warrior);  //TJD
			int moveTo = warrior.getMove();

			// -tjd- Define movement of player
			if (((abs(moveTo - board[warrior.getEntityAt()]) % 100) == 0) && ((abs(moveTo - board[warrior.getEntityAt()]) / 100) == 1) || //vertical
			((abs(moveTo - board[warrior.getEntityAt()]) % 1) == 0) && ((abs(moveTo - board[warrior.getEntityAt()]) / 1) == 1) || //horizontal
			((abs(moveTo - board[warrior.getEntityAt()]) % 99) == 0) && ((abs(moveTo - board[warrior.getEntityAt()]) / 99) == 1) || 
			((abs(moveTo - board[warrior.getEntityAt()]) % 101) == 0) && ((abs(moveTo - board[warrior.getEntityAt()]) / 101) == 1))
			{
				moveTo = moveTo / 100 * boardSize + moveTo % 100;
				warrior.setEntityAt(moveTo);
			}
			//IDLE
			else
			cout << "inValid Move " << endl ;
			}
		}
		//IDLE
		else
		{ choice = 4;}

		system("PAUSE");
		
		//CHECK IF WINNER
		if (warrior.getHp() <= 0)
		{
			system("cls");
			cout << "****************************" << endl;
			cout << "  ~~    Knight Wins!    ~~  " << endl;
			cout << "****************************" << endl;
			break;
		}
		if (knight.getHp() <= 0)
		{
			system("cls");
			cout << "****************************" << endl;
			cout << "  ~~   Warrior Wins!    ~~  " << endl;
			cout << "****************************" << endl;
			break;
		}
	}
}